import { ArchetypeMaskUtil } from "./Archetype";
import { Component } from "./Component";
import { Entity } from "./Entity";

export class TraitArchetypeCache {
    private _cache = new Map<bigint, bigint[]>();

    add(traitMask: bigint, ...archetypeMasks: bigint[]) {
        let cache = this._cache.get(traitMask);
        if (cache) {
            cache.push(...archetypeMasks);
        } else {
            this._cache.set(traitMask, archetypeMasks);
        }
    }

    addEmpty(traitMask: bigint) {
        this._cache.set(traitMask, []);
    }

    get(traitMask: bigint) {
        return this._cache.get(traitMask);
    }

    clear() {
        this._cache.clear();
    }

    // 增量更新
    // 遍历所有的trait，如果trait的mask包含traitMask，则将archetypeMask加入到cache中
    update(archetypeMask: bigint) {
        this._cache.forEach((value, key) => {
            if (ArchetypeMaskUtil.contains(key, archetypeMask)) {
                value.push(archetypeMask);
            }
        });
    }
}


// export class TraitComponentBundleCache {

//     //key是traitMask，value是entityId到component数组的映射
//     private _cache = new Map<bigint, Map<number, Component[]>>();

//     add(traitMask: bigint, entityId: number, component: Component) {
//         let cache = this._cache.get(traitMask);
//         if (cache) {
//         } else {
//             this._cache.set(traitMask, new Map([[entityId, [component]]]));
//         }
//     }

//     get(traitMask: bigint) {
//         return this._cache.get(traitMask);
//     }


    
// }
